import {mapGetters} from "vuex"
import BaseLoad from '@/assets/vue-mixin/BaseLoad'
import supportFacilityInformation from './supporting-facilities-look'
import {get, delFiles} from '../api/project_manage'
import {checkFiles} from '@/api/operate-construction-contract'
import {download} from "~/admin/api/common"
import {downloadBlob} from "~/utils/index"
export default {
    name: "project_look",
    mixins: [BaseLoad],
    created() {
        // 如果是内嵌页面预览隐藏头部和左侧菜单
        const { projectId, frame } =  this.$route.query
        frame && this.$store.commit('resetWebViewStyle',true)
        this.isEdit = projectId
        this.initCitySelector()
        this.projectStreet = this.getDict('streets')
        this.communityNames = this.getDict('community_names')
        this.landUses = this.getDict('land_uses')
        this.projectType = this.getDict('project_types')
        this.manageUnits = this.getDict('administrative_office')
        this.propertyTypes = this.getDict("property_types")
        this.propertyUseTypes = this.getDict("property_use_types")
        this.transferModeOption = this.getDict("transfer_types")
        this.propertyChannels = this.getDict("property_channel")
        this.propForm.landInfo.cityCode = this.cityAssembly[0].value
        this.propForm.landInfo.countyCode = this.districtCollection[0].value
    },
    computed: {
        ...mapGetters({
            cityAssembly: 'getCityAssembly',
            districtCollection: 'getDistrictCollection',
        })
    },
    components: {
        supportFacilityInformation
    },
    data() {
        return {
            propForm: {
                landInfo: {
                    parcelNum: '',
                    parcelArea: '',
                    landUse: '',
                    cityCode: '',
                    countyCode: '',
                    street: '',
                    community: '',
                    addressInfo: ''
                },
                propertyBasic: {
                    projectCode: '',
                    projectName: '',
                    projectArea: '',
                    projectType: '',
                    manageUnit: '',
                    isUpdate: '',
                    constructionUnit: '',
                    contactName: '',
                    contactPhone: '',
                    stage: '',
                    mapCoordinates: '',
                    remarks: '',
                },
                supportingFacilities: [],
                projectMilestones: [],
                totalBuilding: 0
            },
            isEdit: '' ,
            projectId: '' ,
            selected: '',
            milestoneSort: '1',
            milestoneSortIndex: 0,
            addSource: true,
            isAdd: true,
            multipleSelection: [],
            projectStreet: [],
            landUses: [],
            communityNames: [],
            communityNameStreet: [],
            projectType: [],
            manageUnits: [],
            propertyTypes: [],
            propertyUseTypes: [],
            transferModeOption: [],
					  propertyChannels:[]
        }
    },
    methods: {
        //社区联动
        initCommunityName(){
            this.communityNameStreet.length = 0;
            let selectStreet = this.propForm.landInfo.street
            if(selectStreet != '' &&  this.communityNames != null &&  this.communityNames.length > 0){
                this.communityNameStreet = this.communityNames.filter(cn => cn.value.indexOf(selectStreet) != -1)
            }
        },
        //配套信息查看
        handleClickLook(item) {
            this.$refs.supportFacilityInformation.showDialog(item)
        },
        //资料信息选择序号列表
        handleSelectionChange(val) {
            this.multipleSelection = val
        },
        // 调用移除文件
        triggerBeforeRemove(item) {
            this.beforeRemove(item)
        },
        // 移除文件
        beforeRemove(file) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delFiles(file.row.fileBusId, this.modelType).then(resp => {
                    if (resp.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        file.row.original = ''
                        file.row.fileBusId = ''
                        this.isUpload[file.$index] = true
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除失败!'
                        })
                    }
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        //对应value取label
        getCardTypeValue(num, sum){
            let arr = sum.filter(e=>e.value === num)
            if (arr.length > 0) {
                return arr[0].label
            } else {
                return num
            }
        },
           // 初始化
        initCitySelector() {
            const loading = this.$loading({
                lock: true,
                text: '数据加载中'
            });
            get(this.isEdit).then((rep) => {
                this.isAdd = false
                if (rep.data.data.propertyBasic !== null) {
                    this.propForm.propertyBasic = rep.data.data.propertyBasic
                    this.propForm.totalBuilding = rep.data.data.totalBuilding
                    this.projectId = rep.data.data.propertyBasic.projectId
                    if (this.propForm.propertyBasic.addSource === '1') {
                        this.addSource = false
                    }
                }
                if (rep.data.data.projectMilestones !== null) {
                    this.propForm.projectMilestones = rep.data.data.projectMilestones
                    if(this.propForm.projectMilestones.length > 0){
                        this.milestoneSortIndex = this.propForm.projectMilestones.findIndex(pm => pm.milestoneName == this.propForm.propertyBasic.stage)
                        if(this.milestoneSortIndex === -1){
                            this.milestoneSortIndex = 0
                        }
                        this.milestoneSort = (this.milestoneSortIndex+1).toString();
                    }
                }
                if (rep.data.data.landInfo !== null) {
                    this.propForm.landInfo = rep.data.data.landInfo
                    this.initCommunityName()
                }
                if (rep.data.data.supportingFacilities !== null) {
                    this.propForm.supportingFacilities = rep.data.data.supportingFacilities
                }
                this.$set(this.propForm.propertyBasic,'manageUnit',rep.data.data.manageUnit)
                this.$set(this.propForm.propertyBasic,'mapCoordinates',rep.data.data.mapCoordinates)
                loading.close()
            }).catch((e) => {
                console.log(e)
                loading.close()
                this.$message({
                    type: "error",
                    message: "数据获取失败!",
                });
            });

        },
        //关闭
        closeTab() {
            //关闭当前新增页，暂时没找到好办法
            this.$store.commit("delHistoryRoute", this.$route.fullPath);
            // this.$parent.$children[2].closeTab('current')
            //跳转
            this.$router.push("/property-info-sys/project-manage");
        },
        //资料信息文件下载
        handleDownload(item) {
            console.log(item)
            checkFiles(item.fileId).then(resp => {
                if (resp.data.code === 0) {
                    download('/admin/sys-file/download/' + item.fileId, null).then(response => {
                        downloadBlob(response.data, item.original)
                    })
                } else {
                    this.$message({
                        message: `文件不存在！`,
                        type: 'warning'
                    })
                }
            })
        }
    }
}
